/*
 ============================================================================
 Name        : TelCallTelephony_2nd.cpp
 Author      : Michael Liu
 Version     :
 Copyright   : 
 Description : CfCallEngine implementation
 ============================================================================
 */
#ifndef __SERIES60_3X__

#include <eikenv.h>
#include "TelCallTelephony_2nd.h"

TelCallTelephony::TelCallTelephony(TelCallTelephonyNotify& aObserver, RLine& aLine)
:CActive(EPriorityNormal),
iObserver(aObserver),
iLine(aLine),
iState(EWaiting)
{
}

TelCallTelephony::~TelCallTelephony()
{
    //iAppui->SetShowIncomingCall(EFalse);
    Cancel();
    delete iCurrentTelNumber;
    iCurrentTelNumber=NULL;
}

TelCallTelephony* TelCallTelephony::NewL(TelCallTelephonyNotify& aObserver,RLine& aLine)
{
    TelCallTelephony* self = new (ELeave) TelCallTelephony(aObserver, aLine);
    CleanupStack::PushL(self);
    self->ConstructL();
    CleanupStack::Pop(self);
    return self;
}

void TelCallTelephony::ConstructL()
{
    CActiveScheduler::Add(this);
    StartL();
    iCurrentTelNumber=HBufC::NewL(64);
}

//
// CActive
//

void TelCallTelephony::RunL()
{
    if(iStatus.Int() != KErrNone)
    return;

    switch(iState)
    {
        case EWaiting:
        {
            iState = EAnswering;
            StartL();
            break;
        }
        case EAnswering:
        {

            TInt err(0);
            if (&iCall)
            {
                err =iCall.GetStatus(iCallStatus);
                if (err==KErrNone)
                {
                    if ((iCallStatus == RCall::EStatusHangingUp) ||
                            (iCallStatus == RCall::EStatusIdle))
                    {
                        if (&iCall) iCall.Close();
                        iState = EWaiting;
                    }
                    else
                    {
                        iState = EWatching;
                    }
                }
                else
                {
                    iState = EWaiting;
                }
            }
            else
            {
                iState = EWaiting;
            }

            StartL();
            break;
        }
        case EWatching:
        {

            User::LeaveIfError(iCall.GetStatus(iCallStatus));
            if ((iCallStatus == RCall::EStatusHangingUp) ||
                    (iCallStatus == RCall::EStatusIdle))
            {
                iCall.Close();
                iState = EWaiting;
            }
            StartL();
            break;
        }
    }
}

void TelCallTelephony::DoCancel()
{
    Stop();
}

void TelCallTelephony::StartL()
{
    switch(iState)
    {
        case EWaiting:

        iLine.NotifyIncomingCall(iStatus, iCallName);
        SetActive();
        break;
        case EAnswering:
        {
            TInt err = iCall.OpenExistingCall(iLine, iCallName);

            if (err!=KErrNone)
            {
                iState = EWaiting;
                iLine.NotifyIncomingCall(iStatus, iCallName);
                SetActive();
                return;
            }
            iCall.NotifyStatusChange(iStatus, iCallStatus);
            SetActive();
#ifndef __SERIES60_2ND__
            RAdvGsmCall::TRemotePartyInfo myRemotePartyInfo;
            iCall.GetRemotePartyInfo(myRemotePartyInfo);
            YuebaoBuffer<50> callNumber;
            callNumber.Append(myRemotePartyInfo.iNumber.iTelNumber);
            SetCuurentTelNumber(callNumber);
#else
            RMobileCall::TMobileCallInfoV1 callInfo;
            RMobileCall::TMobileCallInfoV1Pckg callInfoPckg(callInfo);
            iCall.GetMobileCallInfo(callInfoPckg);
            YuebaoBuffer<50> callNumber;
            callNumber.Append(callInfo.iRemoteParty.iRemoteNumber.iTelNumber);
#endif
        }
        break;
        case EWatching:
        iCall.NotifyStatusChange(iStatus, iCallStatus);
        SetActive();
        break;
    }
}
void TelCallTelephony::SetCuurentTelNumber(const TDesC& aCurrentTelNumber)
{
    *iCurrentTelNumber=aCurrentTelNumber;
}
const TDesC& TelCallTelephony::GetCuurentTelNumber(void)
{
    return(*iCurrentTelNumber);
}
void TelCallTelephony::Stop()
{
    switch(iState)
    {
        case EWaiting:
        iLine.NotifyIncomingCallCancel();
        break;
        case EAnswering:
        iCall.NotifyStatusChangeCancel();
        iCall.Close();
        break;
        case EWatching:
        iCall.NotifyStatusChangeCancel();
        iCall.Close();
        break;
    }
}
#endif
//End of file
